package com.faxsun.common.currency.dao;

import java.util.List;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.broadleafcommerce.common.currency.domain.BroadleafCurrency;
import org.broadleafcommerce.common.persistence.EntityConfiguration;
import org.hibernate.ejb.QueryHints;
import org.springframework.stereotype.Repository;

import com.faxsun.common.currency.domain.FSCurrencyRate;
import com.faxsun.common.currency.domain.FSCurrencyRateImpl;

@Repository("fsCurrencyRateDao")
public class FSCurrencyRateDaoImpl implements FSCurrencyRateDao {

	@PersistenceContext(unitName = "blPU")
	protected EntityManager em;

	@Resource(name = "blEntityConfiguration")
	protected EntityConfiguration entityConfiguration;

	@Override
	public List<FSCurrencyRate> readBySourceCode(BroadleafCurrency code) {

		Query query = em
				.createQuery("SELECT fsCurrencyRate FROM com.faxsun.common.currency.domain.FSCurrencyRate fsCurrencyRate"
						+ " where fsCurrencyRate.sourceCurrency=:sourceCurrency"
						+ " order by fsCurrencyRate.date desc");
		query.setParameter("sourceCurrency", code);
		query.setHint(QueryHints.HINT_CACHEABLE, true);
		query.setHint(QueryHints.HINT_CACHE_REGION, "query.FSCurrencyRate");

		@SuppressWarnings("unchecked")
		List<FSCurrencyRate> results = query.getResultList();

		return results;
	}

	@Override
	public List<FSCurrencyRate> readByDestinationCode(BroadleafCurrency code) {
		Query query = em
				.createQuery("SELECT fsCurrencyRate FROM com.faxsun.common.currency.domain.FSCurrencyRate fsCurrencyRate"
						+ " where fsCurrencyRate.destinationCurrency=:destinationCurrency"
						+ " order by fsCurrencyRate.date desc");
		query.setParameter("destinationCurrency", code);
		query.setHint(QueryHints.HINT_CACHEABLE, true);
		query.setHint(QueryHints.HINT_CACHE_REGION, "query.FSCurrencyRate");

		@SuppressWarnings("unchecked")
		List<FSCurrencyRate> results = query.getResultList();

		return results;
	}

	/**
	 * 根据原始货币和目标货币，从数据库读取对应的汇率
	 * 
	 * @source 原始货币
	 * @destiination 目标货币
	 * @return 汇率 {@link FSCurrencyRateImpl}
	 */
	@Override
	public FSCurrencyRate readByCodes(BroadleafCurrency source,
			BroadleafCurrency destination) {
		// TODO Auto-generated method stub
		Query query = em
				.createQuery("SELECT fsCurrencyRate FROM com.faxsun.common.currency.domain.FSCurrencyRate fsCurrencyRate"
						+ " where fsCurrencyRate.sourceCurrency=:sourceCurrency"
						+ " and fsCurrencyRate.destinationCurrency=:destinationCurrency"
						+ " order by fsCurrencyRate.date desc");
		query.setParameter("sourceCurrency", source);
		query.setParameter("destinationCurrency", destination);
		query.setHint(QueryHints.HINT_CACHEABLE, true);
		query.setHint(QueryHints.HINT_CACHE_REGION, "query.FSCurrencyRate");

		@SuppressWarnings("unchecked")
		List<FSCurrencyRate> results = query.getResultList();

		if (results.size() < 1)
			return null;

		return results.get(0);
	}

	@Override
	public FSCurrencyRate save(FSCurrencyRate currencyRate) {
		return em.merge(currencyRate);
	}

	@Override
	public FSCurrencyRate create() {
		final FSCurrencyRate rate = ((FSCurrencyRate) entityConfiguration
				.createEntityInstance("com.faxsun.common.currency.domain.FSCurrencyRate"));

		return rate;
	}

	@Override
	public FSCurrencyRate update(FSCurrencyRate currencyRate) {
		return em.merge(currencyRate);
	}

}
